package com.hotacorp.opencloud.userservice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.AddGroup;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.UpdateGroup;
import com.hotacorp.opencloud.userservice.entity.SysMenuEntity;
import com.hotacorp.opencloud.userservice.service.SysMenuService;

/**
 * 菜单管理
 *
 * @author lwg
 * @email lwg@gmail.com
 * @date 2019-12-25 11:52:19
 */
@RestController
public class SysMenuController extends AbstractController {
    @Autowired
    private SysMenuService sysMenuService;

	/**
	 * 导航菜单
	 */
	@GetMapping("/sysmenu/nav")
	public Result<?> nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId(), getUsername());
		Set<String> permissions = getAuthoritys();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("menuList", menuList);
		data.put("permissions", permissions);
		return Result.succeed("suc", data);
	}

	/**
     * 列表
     */
	@RequestMapping(value = "/sysmenu/list", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysmenu:list')")
    public Result<?> list(){
    	List<SysMenuEntity> page = sysMenuService.listAll();
        page.forEach(action->{
        	SysMenuEntity ee = page.stream().filter(all1->all1.getMenuId().longValue()==action.getParentId().longValue()).findFirst().orElse(null);
        	if (ee != null) {
        		action.setParentName(ee.getName());
        	} else {
        		action.setParentName("一级菜单");
        	}
        });
        return Result.succeed("succeed", page);
    }

	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/sysmenu/select")
	@PreAuthorize("hasAuthority('userservice:sysmenu:select')")
	public Result<?> select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return Result.succeed("succeed", menuList);
	}

    /**
     * 信息
     */
    @RequestMapping(value = "/sysmenu/info/{menuId}", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysmenu:info')")
    public Result<?> info(@PathVariable("menuId") Long menuId){
		SysMenuEntity sysMenu = sysMenuService.getById(menuId);

        return Result.succeed("succeed", sysMenu);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/sysmenu/save", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysmenu:save')")
    public Result<?> save(@RequestBody @Validated(AddGroup.class) SysMenuEntity sysMenu){
		sysMenuService.save(sysMenu);

        return Result.succeed();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/sysmenu/update", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysmenu:update')")
    public Result<?> update(@RequestBody @Validated(UpdateGroup.class) SysMenuEntity sysMenu){
		sysMenuService.updateById(sysMenu);

        return Result.succeed();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/sysmenu/delete/{menuId}", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:sysmenu:delete')")
    public Result<?> delete(@PathVariable("menuId") long menuId){
		int aaa = sysMenuService.delete(menuId);
		if (aaa != 0) {
			return Result.failed("请先删除子目录、菜单或按钮");
		} 

		return Result.succeed();
    }

}
